/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
/**
 * \file
 * \brief Main for USBPanicButton to be used with MaCI_EmergencyStopGUI. communicates with GUI through pipes.
 * \author Matthieu Myrsky <matthieu.myrsky@tkk.fi>
 */
#include "USBPanicButton.hpp"
#include "USBPanicButtonProtocol.hpp"
static int iWritePipeInt;
static int iReadPipeInt;
#include <sstream>
#include <poll.h>
#include <stdio.h>
class CCallBack : public CUSBPanicButtonCallback
{
public:
  CCallBack(){}
  ~CCallBack(){}

protected:
  void OnUSBPanicButtonPressed(){
    std::ostringstream stm;
    stm << KMessageUSBEmergencyKeyPressed;
    write(iWritePipeInt,stm.str().c_str(),1);
    dPrint(1,"Panic button pressed received at callback");
  }


};

int main(int argc,  char **argv){
  debugInit();
  debugSetGlobalDebugLvl(3);
  bool running = true;
  dPrint(1,"USBPanicButton executed\n");
  if(argc != 3){
    dPrint(1,"no enough parameters.Usage: %s <readpipeint> <writepipeint>",*argv);
    std::ostringstream stm;
    stm << KMessageFailedToOpenUSBPanicButton;
    write(iWritePipeInt,stm.str().c_str(),1);
    return 0;
  }
  CUSBPanicButton button;
  CCallBack cb;

  iWritePipeInt = atoi(argv[2]);
  iReadPipeInt = atoi(argv[1]);
  button.SetUSBPanicButtonCallback(&cb);
   
  if(button.Open()){
    std::ostringstream stm;
    stm << KMessageSuccesfullyOpenedUSBPanicButton;
    write(iWritePipeInt,stm.str().c_str(),1);
    char buf;
    //Start
    while(running){
      //Then wait for close command
      struct pollfd pfds[1] = {{ iReadPipeInt, POLLIN, 0 }};
      int result = poll(pfds, 1, 1000);
      
      if (result < 0) {
        dPrintLE(3,"poll() failed for 'read' file descriptor (%d)",
                 iReadPipeInt);
        
      }else if(result > 0){
        if(read(iReadPipeInt,&buf,1) >0){
          dPrint(1,"GOT MESSAGE");
          if(atoi((const char *)&buf) == KMessageCloseUSBPanicButton){
            running = false;
          }
            
        }else{
            dPrint(1,"didn't recognize message %d",atoi((const char *)&buf));
        }
      }
     
    }


  }else{
    std::ostringstream stm;
    dPrint(1,"Sending reply to %d", iWritePipeInt);
    stm << KMessageFailedToOpenUSBPanicButton;
    write(iWritePipeInt,stm.str().c_str(),1);
    dPrint(1,"Failed to open panic button");
  }
  dPrint(1,"Closing USBPanic button");
  button.Close();
  bool waitForKill = true;
  while(waitForKill);
  return 1;
}
